package com.fs.sys.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fs.common.annotations.Log;
import com.fs.common.bean.LogType;
import com.fs.sys.bean.dto.RoleDTO;
import com.fs.sys.bean.query.RoleQuery;
import com.fs.sys.bean.req.RoleAddReq;
import com.fs.sys.bean.req.RoleUpdateReq;
import com.fs.sys.bean.resp.RoleResourceInfo;
import com.fs.sys.entityservice.RoleEntityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/role")
@Slf4j
@Tag(name = "角色控制器")
public class RoleController {
    @Autowired
    private RoleEntityService roleEntityService;

    @PostMapping("add")
    @Operation(summary = "新增")
    @Log(value = "角色", op = LogType.NEW)
    public void add(@RequestBody @Validated RoleAddReq req) {
        RoleDTO dto =  new RoleDTO();
        BeanUtils.copyProperties(req, dto);
        roleEntityService.insert(dto);
       
    }

    @PutMapping("update")
    @Operation(summary = "更新")
    @Log(value = "角色", op = LogType.EDIT)
    public void update(@RequestBody @Validated RoleUpdateReq req) {
        RoleDTO dto = new RoleDTO();
        BeanUtils.copyProperties(req, dto);
        roleEntityService.update(dto);
       
    }

    @DeleteMapping("delete/{id}")
    @Operation(summary = "删除")
    @Log(value = "角色", op = LogType.DELETE)
    public void delete(@PathVariable("id") String id) {
        roleEntityService.delete(id);
       
    }

    @PostMapping("page-query")
    @Operation(summary = "查询-分页")
    public IPage<RoleDTO> pageQuery(@RequestBody RoleQuery query) {
        return roleEntityService.pageQuery(query);
    }

    @PostMapping("query")
    @Operation(summary = "查询-不分页")
    public List<RoleDTO> query(@RequestBody RoleQuery query) {
        return roleEntityService.query(query);
    }

    @GetMapping("resources")
    @Operation(summary = "查找角色菜单信息")
    public List<RoleResourceInfo> getRoleMenus(
            @RequestParam(value = "appId", defaultValue = "sys") String appId,
            String roleId) {
        return roleEntityService.getRoleMenus(appId, roleId);
    }
}
